import { h, render } from 'vue'
import XtxConfirm from './XtxConfirm.vue'
type Props = {
    text: string
    title?: string
}

// 创建一个div
const div = document.createElement('div')
// 给div 加个 class
div.setAttribute('class', 'xtx-confirm-container')
// 插入到body
document.body.appendChild(div)

export default function Confirm({ text, title }: Props) {
    return new Promise((resolve, reject) => {
        const confirmCallback = () => {
            render(null, div)
            resolve(undefined)
        }
        const cancelCallback = () => {
            render(null, div)
            reject(undefined)
        }
        // 给XtxConfirm组件 抛了两个函数
        const vNode = h(XtxConfirm, {
            title,
            text,
            confirmCallback, // 确定
            cancelCallback // 取消
        })
        render(vNode, div)
    })
}